﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;

namespace NSATBPortal
{
    public partial class BaseStateParameterDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string sOp = Request.QueryString["Op"].ToString();
                string sParentName = Server.UrlDecode(Request.QueryString["ParentName"].ToString());

                linkParent.HRef += "?ID=" + Request.QueryString["ParentID"].ToString()
                                    + "&Name=" + Request.QueryString["ParentName"].ToString();

                txtValue.Enabled = false;

                if (sOp == "Edit")
                {
                    string sName = Server.UrlDecode(Request.QueryString["Name"].ToString());
                    lblParent.Text = "Edit Parameter " + sName + " for " + sParentName;
                }
                else
                {
                    lblParent.Text = "Add Parameter " + " for " + sParentName;
                }

                if (sOp == "Edit")
                {
                    string sName = Server.UrlDecode(Request.QueryString["Name"].ToString());
                    string sValue = Server.UrlDecode(Request.QueryString["Value"].ToString());
                    if (sValue == "NULL")
                        sValue = "";
                    ListItem li = new ListItem(sName, sName, true);
                    dropdownName.Items.Add(li);
                    dropdownName.SelectedIndex = 0;

                    DataTable dtParamValues;
                    using (ATBSchemaDB objATBDB = new ATBSchemaDB())
                    {
                        dtParamValues = objATBDB.GetBaseStateParamValidValues(sName);
                    }

                    if (dtParamValues.Rows.Count > 0)
                    {
                        txtValue.Visible = false;
                        dropdownValue.Visible = true;
                        dropdownValue.DataValueField = "Value";
                        dropdownValue.DataSource = dtParamValues;
                        dropdownValue.DataBind();
                        if (sValue.Trim().Length > 0)
                            dropdownValue.SelectedValue = sValue;
                    }
                    else
                    {
                        dropdownValue.Visible = false;
                        txtValue.Visible = true;
                        txtValue.Enabled = true;
                        txtValue.Text = sValue;
                    }

                }
                else
                {
                    cmdSave.Text = "Add";

                    string sVal = Request.QueryString["ParentID"].ToString();
                    int iBaseStateID = Convert.ToInt32(sVal);

                    DataTable dtResults;
                    using (ATBSchemaDB objATBDB = new ATBSchemaDB())
                    {
                        dtResults = objATBDB.GetParamsAvailForDefBaseState(iBaseStateID);
                    }

                    dropdownName.Enabled = true;
                    dropdownName.DataValueField = "Name";
                    dropdownName.DataSource = dtResults;
                    dropdownName.DataBind();
                    dropdownName.Items.Add(new ListItem("[ Select ]", "0"));
                    dropdownName.SelectedValue = "0";

                    dropdownValue.Visible = false;
                }
            }
        }

        protected void dropdownName_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (dropdownName.Enabled)
            {
                string sName = dropdownName.SelectedValue;
                string sDefaultValue = "";
                using (ATBSchemaDB objATBDB = new ATBSchemaDB())
                {
                    sDefaultValue = objATBDB.GetBaseStateParamDefaultValue(sName);
                    if (sDefaultValue.ToLower().Equals("null") || string.IsNullOrEmpty(sDefaultValue.Trim()))
                        sDefaultValue = string.Empty;
                }
                DataTable dtParamValues;
                using (ATBSchemaDB objATBDB = new ATBSchemaDB())
                {
                    dtParamValues = objATBDB.GetBaseStateParamValidValues(sName);
                }
                
                if (dtParamValues.Rows.Count > 0)
                {
                    txtValue.Visible = false;
                    dropdownValue.Visible = true;
                    dropdownValue.DataValueField = "Value";
                    dropdownValue.DataSource = dtParamValues;
                    dropdownValue.DataBind();
                    dropdownValue.Items.Add(new ListItem("[ Select ]"));
                    if(string.IsNullOrEmpty(sDefaultValue))
                        dropdownValue.SelectedValue = "[ Select ]";
                    else
                        dropdownValue.SelectedValue = sDefaultValue;
                }
                else
                {
                    dropdownValue.Visible = false;
                    txtValue.Visible = true;
                    txtValue.Enabled = true;
                    txtValue.Text = sDefaultValue;
                }
            }
        }

        protected void cmdSave_Click(object sender, EventArgs e)
        {
            string sOp = Request.QueryString["Op"].ToString();
            string sUserID = Request.LogonUserIdentity.Name;
            int iBaseStateID = Convert.ToInt32(Server.UrlDecode(Request.QueryString["ParentID"].ToString()));
            if (sOp != "Edit")
            {
                if (dropdownName.SelectedValue == "[ Select ]")
                {
                    lblErrorMessage.Text = "ERROR: You must select a parameter";
                    return;
                }
                if (dropdownValue.Visible)
                {
                    if (dropdownValue.SelectedValue == "[ Select ]")
                    {
                        lblErrorMessage.Text = "ERROR: You must select a parameter value";
                        return;
                    }
                }
                else
                {
                    if (txtValue.Text.Trim().Length == 0)
                    {
                        lblErrorMessage.Text = "ERROR: You must enter a parameter value";
                        return;
                    }
                }
            }

            string sValue;
            if (txtValue.Visible)
                sValue = txtValue.Text;
            else
                sValue = dropdownValue.SelectedValue;

            using (ATBSchemaDB objATBDB = new ATBSchemaDB())
            {
                objATBDB.DefBaseStateUpdateParam(iBaseStateID, dropdownName.Text, sValue, sUserID);
            }

            Response.Redirect(linkParent.HRef);
        }

        protected void cmdCancel_Click(object sender, EventArgs e)
        {

            Response.Redirect(linkParent.HRef);

        }
    }
}